import { httpDelete, httpGet, httpPost, httpPut } from '@/http'

/**
 * 接口生成
 * @param svc 服务地址
 * @constructor
 */
export const useFormApi = svc => {
  /**
   * 创建
   * @param params {Record<string,any>?} 路径参数
   * @returns {Promise<ResponseResult>}
   */
  const create = params => httpGet(`${svc}/create`, params)
  /**
   * 获取分页
   * @param page {Record<string,any>?} 分页对象
   * @returns {Promise<ResponseResult>}
   */
  const getPage = page => httpPost(`${svc}/page`, page)
  /**
   * 获取树
   * @param params {Record<string,any>?} 路径参数
   * @returns {Promise<ResponseResult>}
   */
  const getTree = params => httpGet(`${svc}/tree`, params)
  /**
   * 保存
   * @param entity {Record<string,any>} 实体
   * @returns {Promise<ResponseResult>}
   */
  const save = entity => httpPost(`${svc}`, entity)
  /**
   * 加载
   * @param id {string|number} 实体 id
   * @returns {Promise<ResponseResult>}
   */
  const load = id => httpGet(`${svc}/edit/${id}`)
  /**
   * 更新
   * @param entity {Record<string,any>} 实体
   * @returns {Promise<ResponseResult>}
   */
  const update = entity => httpPut(`${svc}/${entity.id}`, entity)
  /**
   * 删除
   * @param ids {string} 要删除的实体 id
   * @returns {Promise<ResponseResult>}
   */
  const del = ids => httpDelete(`${svc}`, { ids }, null, true)

  return { create, getPage, getTree, save, load, update, del }
}

export const useFlowApi = svc => {
  /**
   * 提交
   * @param id {string|number} 实体 id
   * @returns {Promise<ResponseResult>}
   */
  const submit = id => httpPost(`${svc}/submit/${id}`)
  /**
   * 撤回
   * @param id {string|number} 实体 id
   * @returns {Promise<ResponseResult>}
   */
  const revoke = id => httpPost(`${svc}/revoke/${id}`)
  /**
   * 完成
   * @param id {string|number} 实体 id
   * @returns {Promise<ResponseResult>}
   */
  const finish = id => httpPost(`${svc}/finish/${id}`)

  const { create, getPage, getTree, save, load, update, del } = useFormApi(svc)

  return { create, getPage, getTree, save, load, update, del, submit, revoke, finish }
}
